<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>div拖拽事件</title>
    <style>
        #box {
            width: 200px;
            height: 200px;
            background-color: aquamarine;
            position: absolute;
        }
    </style>
    <script>
        window.onload = function() {
            var oDiv = document.getElementById('box');
            var disX = 0;
            var disY = 0
            oDiv.onmousedown = function(ev) {
                var oEvent = ev || event;
                disX = oEvent.clientX - oDiv.offsetLeft;
                disY = oEvent.clientY - oDiv.offsetTop;
                document.onmousemove = function(ev) {
                        var oEvent = ev || event;
                        // 加上滚动条防止页面有滚动条时造成脱轨
                        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
                        var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
                        var l = oEvent.clientX - disX + scrollLeft;
                        var t = oEvent.clientY - disY + scrollTop;
                        if (l < 0) {
                            l = 0;
                        } else if (l > document.documentElement.clientWidth - oDiv.offsetWidth) {
                            l = document.documentElement.clientWidth - oDiv.offsetWidth
                        }
                        if (t < 0) {
                            t = 0;
                        } else if (t > document.documentElement.clientHeight - oDiv.offsetHeight) {
                            t = document.documentElement.clientHeight - oDiv.offsetHeight
                        }
                        oDiv.style.left = l + 'px';
                        oDiv.style.top = t + 'px';
                        console.log('x', l, '     ' + 'y' + t);
                    }
                    // 防止火狐低版本有bug
                    // return false;
            }
            document.onmouseup = function() {
                document.onmousemove = null;
                // document.onmouseup = null;
            }

            // 移动端拖动事件
            oDiv.ontouchstart = function(ev) {
                var oEvent = ev || window.event;
                var touch = oEvent.targetTouches[0];
                disX = touch.clientX - oDiv.offsetLeft;
                disY = touch.clientY - oDiv.offsetTop;
                document.ontouchmove = function(ev) {
                    var oEvent = ev || window.event;
                    var touch = oEvent.targetTouches[0];
                    // 加上滚动条防止页面有滚动条时造成脱轨
                    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
                    var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
                    var l = touch.clientX - disX + scrollLeft;
                    var t = touch.clientY - disY + scrollTop;
                    if (l < 0) {
                        l = 0;
                    } else if (l > document.documentElement.clientWidth - oDiv.offsetWidth) {
                        l = document.documentElement.clientWidth - oDiv.offsetWidth
                    }
                    if (t < 0) {
                        t = 0;
                    } else if (t > document.documentElement.clientHeight - oDiv.offsetHeight) {
                        t = document.documentElement.clientHeight - oDiv.offsetHeight
                    }
                    oDiv.style.left = l + 'px';
                    oDiv.style.top = t + 'px';
                    console.log('x', l, '     ' + 'y' + t);
                }
            }
            document.ontouchend = function() {
                document.ontouchmove = null;
            }
        }
    </script>
</head>

<body style="height: 400px;">
    <div id="box"></div>
</body>

</html>